class Solution:
    def bestTeamScore(self, scores: list, ages: list) -> int:
        players = [i for i in range(len(scores))]
        players.sort(key=lambda x: (ages[x], scores[x]), reverse=True)
        dp = [0 for _ in range(len(players))]
        ans = 0
        for i in range(len(players)):
            score = scores[players[i]]
            dp[i] = score
            for j in range(i - 1, -1, -1):
                if scores[players[j]] >= score:
                    dp[i] = max(dp[i], dp[j] + score)
            ans = max(ans, dp[i])

        return ans
